package cn.icanci.loopstack.ddk.admin.biz.service.impl;

import cn.hutool.json.JSONUtil;
import cn.icanci.loopstack.ddk.admin.biz.event.log.LogEvent;
import cn.icanci.loopstack.ddk.admin.biz.mapping.config.TeamMapping;
import cn.icanci.loopstack.ddk.admin.biz.service.BaseService;
import cn.icanci.loopstack.ddk.admin.biz.service.TeamService;
import cn.icanci.loopstack.ddk.admin.dal.mapper.entity.TeamDO;
import cn.icanci.loopstack.ddk.admin.dal.mapper.mapper.TeamMapper;
import cn.icanci.loopstack.ddk.common.enums.LogOperatorTypeEnum;
import cn.icanci.loopstack.ddk.common.enums.ModuleTypeEnum;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.TextValue;
import cn.icanci.loopstack.ddk.common.model.config.TeamVO;
import cn.icanci.loopstack.ddk.common.utils.PaginatorUtils;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/06 21:07
 */
@Service
public class TeamServiceImpl extends BaseService<TeamVO> implements TeamService {
    @Resource
    private TeamMapper  teamMapper;
    @Resource
    private TeamMapping teamMapping;

    @Override
    public PageList<TeamVO> queryPage(TeamVO vo, int currentPage, int pageSize) {

        vo.setEnv(getEnv());
        vo.setIsDelete(null);

        currentPage = Math.max(currentPage - 1, 0);

        TeamDO ddkTeam = teamMapping.vo2do(vo);
        List<TeamDO> ret = teamMapper.selectPage(ddkTeam, currentPage, pageSize);

        return PaginatorUtils.formatPaginator(teamMapping.dos2vos(ret), currentPage, pageSize, teamMapper.total(ddkTeam));
    }

    @Override
    public void save(TeamVO vo) {
        if (doInsert(vo)) {
            beforeInsert(vo);
            TeamDO teamDO = teamMapping.vo2do(vo);
            teamMapper.insert(teamDO);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_TEAM, JSONUtil.toJsonStr(teamDO), LogOperatorTypeEnum.CREATE), false);
        } else {
            beforeUpdate(vo);
            TeamDO teamDO = teamMapping.vo2do(vo);
            teamMapper.update(teamDO);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_TEAM, JSONUtil.toJsonStr(teamDO), LogOperatorTypeEnum.UPDATE), false);
        }
    }

    @Override
    public TeamVO queryByTeamId(String teamId) {
        TeamDO teamDO = teamMapper.selectByTeamId(teamId, getEnv());
        return teamMapping.do2vo(teamDO);
    }

    @Override
    public TeamVO queryByTeamUuid(String teamUuid) {
        TeamDO teamDO = teamMapper.selectByTeamUUid(teamUuid, getEnv());
        return teamMapping.do2vo(teamDO);
    }

    @Override
    public TeamVO queryByTeamName(String teamName) {
        TeamDO teamDO = teamMapper.selectByTeamName(teamName, getEnv());
        return teamMapping.do2vo(teamDO);
    }

    @Override
    public List<TextValue> loadSelector() {
        TeamDO teamDO = new TeamDO();
        teamDO.setEnv(getEnv());

        List<TeamDO> teams = teamMapper.selectList(teamDO);

        if (CollectionUtils.isEmpty(teams)) {
            return Lists.newArrayList();
        }

        List<TeamVO> teamVOS = teamMapping.dos2vos(teams);
        List<TextValue> textValues = Lists.newArrayList();
        for (TeamVO teamVO : teamVOS) {
            String label;
            if (isDeleted(teamVO)) {
                label = String.format(DELETED_FORMAT, teamVO.getTeamName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, teamVO.getTeamName());
            }
            String value = teamVO.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }
}
